package cn.zkw.controller;

import cn.zkw.service.*;
import cn.zkw.vo.Article;
import cn.zkw.vo.Comment;
import cn.zkw.vo.Label;
import cn.zkw.vo.Notice;
import net.sf.json.JSONObject;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Controller
public class ControllerUtil {
    @Autowired
    ArticleService articleService;

    @Autowired
    CommentService commentService;

    @Autowired
    LabelService labelService;

    @Autowired
    NoticeService noticeService;

    @Autowired
    UserService userService;
    /**
     * 前台查看文章页面跳转
     * @param article_id
     * @param request
     * @return
     */
    @RequestMapping("/front/article")
    public String article(Integer article_id, HttpServletRequest request,Model model){
        request.getSession().setAttribute("article_id",article_id); //文章id
        Article article = articleService.selectArticleById(article_id); //查找文章
        request.getSession().setAttribute("article",article);
        model.addAttribute("article_content",article.getArticle_content()); //文章内容
        List<Comment> listComment = commentService.selectCommentByArticleId(article_id);
        Iterator<Comment> iterator = listComment.iterator();
        //获取文章标签集合
        List<Label> labels = labelService.selectLabelByArticle_id(article_id);
        request.setAttribute("labels",labels);
        //如果有用户登陆，根据文章标签集合与用户id，记录用户文章喜好
        Integer user_id = null;
        if(request.getSession().getAttribute("user_id")!=null){
            user_id = (Integer)request.getSession().getAttribute("user_id");
        }
        if(user_id!=null){
            for(int i=0;i<labels.size();i++){
                Map<String,Integer> map = new HashMap<>();
                map.put("user_id",user_id);
                map.put("label_id",labels.get(i).getLabel_id());
                userService.addUser_like_OR_inreaseClick_Count(map); //增加其他标签权值
                userService.minusClick_count(map); //减少其他标签权值
            }
        }
        //浏览量更新
        article.setArticle_views(article.getArticle_views()+1);
        articleService.updateView(article);
        model.addAttribute("listComment",listComment);

        //加载相关推荐
        Map<String,Object> map = new HashedMap();
        map.put("num",1);
        String keyword = (String) request.getSession().getAttribute("keyword");
        map.put("keyword",labels.get(0).getLabel_name());
        List<Article> articles = articleService.splitArticleByKeyWord(map);
        if(labels.size()>=2){
            map.put("keyword",labels.get(1).getLabel_name());
        }else{
            map.put("keyword",labels.get(0).getLabel_name());
            map.put("num",2);
        }
        articles.addAll(articleService.splitArticleByKeyWord(map));
        if(articles.size()>8){
            articles = articles.subList(0,8);  //只要8条
        }
        request.setAttribute("recommend",articles);
        return "front/article";
    }


    /**
     * 根据id查看文章接口
     */
    @RequestMapping(value = "/getArticle", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public @ResponseBody Object getArticle(HttpServletRequest request){
        JSONObject jsonObject = new JSONObject();
        Integer article_id =  (Integer)request.getSession().getAttribute("article_id");  //从/front/article中添加的article_id获取
        Article article =  articleService.selectArticleById(article_id);   //根据id查询文章
        jsonObject.put("article",article);
        jsonObject.put("article_content",article.getArticle_content());
        return jsonObject;
    }

    /**
     * 标签云跳转
     * @param request
     * @return
     */
    @RequestMapping("/front/tags")
    public String tags(HttpServletRequest request){
        List<Label> labels = labelService.findAll();
        StringBuffer labelstr = new StringBuffer();
        for (int i=0;i<labels.size();i++){
            labelstr.append(labels.get(i).getLabel_name()).append(" ");
        }
        request.setAttribute("labelstr",labelstr);
        return "/front/tags";
    }

    /**
     * 查看公告跳转
     * @param request
     * @return
     */
    @RequestMapping("/front/notice")
    public String notice(HttpServletRequest request,Integer notice_id){
        Notice notice = noticeService.selectById(notice_id);
        request.setAttribute("notice",notice);
        return "/front/notice";
    }
}
